package com.kan.awaits.mongo.meta;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.List;

import com.kan.awaits.mongo.annotations.Collection;
import com.kan.awaits.mongo.annotations.Entity;
import com.kan.awaits.mongo.annotations.Property;

/**
 * 
 * @author Elva
 * @date 2011-4-13
 * @version 1.0
 */
public class EntityMetaImpl implements EntityMeta
{
	String entityName;

	Class<?> entityType;

	List<EntityPropertyMeta> entityPropertyMetas;

	CollectionMeta collectionMeta;

	public EntityMetaImpl(Class<?> type) throws Exception
	{
		this.entityType = type;

		if (!type.isAnnotationPresent(Entity.class))
		{
			throw new Exception("type [" + type + "] is not an Entity! use Annotation @Eneity to Present an Entity!");
		}
		Entity entity = entityType.getAnnotation(Entity.class);

		entityName = entity.value().trim();

		if (entityName == null || "".equals(entityName))
		{
			entityName = entityType.getSimpleName();
		}
		if (entityType.isAnnotationPresent(Collection.class))
		{
			collectionMeta = new CollectionMeta(entityType.getAnnotation(Collection.class));
		} else
		{

		}

		Field[] fields = entityType.getDeclaredFields();
		for (Field field : fields)
		{
			field.isAnnotationPresent(Property.class);
		}
		BeanInfo beanInfo = Introspector.getBeanInfo(type, Object.class);
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor p : propertyDescriptors)
		{
			p.getName();
			Property pa = p.getReadMethod().getAnnotation(Property.class);
		}
	}

	public String getEntityName()
	{
		return entityName;
	}

	public List<EntityPropertyMeta> getEntityPropertyMetas()
	{
		return entityPropertyMetas;
	}

	public Class<?> getEntityType()
	{
		return entityType;
	}
}
